extends Camera2D
var dragging = false
var last_mouse_position = Vector2()

# 缩放速度
const ZOOM_SPEED = 0.02
# 最小和最大缩放值
const MIN_ZOOM = 0.5
const MAX_ZOOM = 2

func _ready():
		# 初始化缩放值
	zoom = Vector2(1.0,1.0)
	
func _input(event):
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_WHEEL_UP:
			zoom_out()
		elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
			zoom_in()
			
	if event is InputEventMouseButton:
		if event.button_index == MOUSE_BUTTON_LEFT:
			if event.pressed:
				# 开始拖动
				dragging = true
				last_mouse_position = event.position
			else:
				# 结束拖动
				dragging = false

	if event is InputEventMouseMotion and dragging:
		var mouse_delta = event.position - last_mouse_position
		position -= mouse_delta*(Vector2(1.0,1.0))
		last_mouse_position = event.position
		
func zoom_in():
	var new_zoom = zoom - Vector2(ZOOM_SPEED, ZOOM_SPEED)
	zoom = Vector2(clamp(new_zoom.x, MIN_ZOOM, MAX_ZOOM), clamp(new_zoom.y, MIN_ZOOM, MAX_ZOOM))

func zoom_out():
	var new_zoom = zoom + Vector2(ZOOM_SPEED, ZOOM_SPEED)
	zoom = Vector2(clamp(new_zoom.x, MIN_ZOOM, MAX_ZOOM), clamp(new_zoom.y, MIN_ZOOM, MAX_ZOOM))
